package com.workflow.action;

import java.util.List;
import java.util.Set;

import com.base.service.CardTypeService;
import com.base.service.DepartmentService;
import com.base.service.DictService;
import com.base.service.FlowaudithistoryService;
import com.base.service.FlowworkitemService;
import com.base.service.PermissionService;
import com.common.bean.Cardmaintype;
import com.common.bean.Datadict;
import com.common.bean.Department;
import com.common.bean.Flowtemplatedetail;
import com.common.bean.Flowworkitem;
import com.common.bean.Personnel;
import com.common.util.PagerModel;
import com.common.util.StringToken;
import com.core.ssh2.AbstractActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.workflow.bean.Applydept;
import com.workflow.service.ApplydeptService;

public class ApplydeptManagerAction extends AbstractActionSupport implements Preparable{
	private static final long serialVersionUID = -7308002455401651629L;

	private ApplydeptService applydeptService;
	
	private DepartmentService departmentService;
	
	private DictService dictService;
	
	private FlowworkitemService flowworkitemService;
	
	private CardTypeService cardTypeService;
	
	private FlowaudithistoryService flowaudithistoryService;
	
	private PermissionService permissionService;
	
	private Applydept applydept;
	
	private String applyno;
	
	private PagerModel pm;
	
	private String deptcode;
	
	private List<Department> departmentByDepartmentDeptcodelist;
	
	private List<Datadict> datadictByMarklist;
	
	private List<Datadict> datadictByUnitlist;
	
	private List<Datadict> datadictByPrilist;
	
	private List<Datadict> datadictByStatuslist;
	
	private List<Cardmaintype> cardmaintypelist;
	
	@SuppressWarnings("unchecked")
	public void prepare() throws Exception {
//		需要判断部门，部门生成策略，取审核流程对应部门，该部门应该是泛指，不应明确，取步骤表的最大步骤部门
		departmentByDepartmentDeptcodelist = departmentService.findAllleafDepartment();
		cardmaintypelist = cardTypeService.findAllCardmaintype();
		datadictByUnitlist = dictService.findDictDetail(StringToken.DICT_WORK_UNIT);
		datadictByPrilist = dictService.findDictDetail(StringToken.DICT_WORK_PRI);
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String goAddApplydept() throws Exception {
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String addApplydept() throws Exception {
		//get personnel,department
		//get flowworkitem 、flowtemplatedetail
		
		Personnel personnel = (Personnel) getSession().get(StringToken.CURRENT_LOGIN);
		Flowworkitem flowworkitem = null;
		/*
		 admin 账户为超级管理员用户！测试专用。		 
		 */
		String deptcode = "";
		Department department = new Department();
		if("admin".equals(personnel.getLoginname())){
			deptcode = "000103"; //测试部门代号
			department.setDeptcode(deptcode);
		}else {
			deptcode = personnel.getDepartment().getDeptcode();
			department = personnel.getDepartment();
		}
		flowworkitem = flowworkitemService.finaFlowworkitem(deptcode
				, applydept.getCardmaintype().getCardmaintypeno()/*类型一致*/
				, StringToken.DICT_FLOWFLAG_APPLYDEPT);
		
		applydept.setPersonnel(personnel);
		applydept.setDepartmentByDeptcode(department);
		applydept.setFlowworkitem(flowworkitem);
		
		Datadict datadict = new Datadict();
		datadict.setTypeid(StringToken.DICT_WORK_MARK_NO);
		applydept.setDatadictByMark(datadict);//是否超时、默认为否
		
		Object obj = null;
		Set<Flowtemplatedetail>	set = flowworkitem.getFlowtemplatedetails();
		for(Flowtemplatedetail flowtemplatedetail : set) {
			if(flowtemplatedetail.getSeq() == 1l) {
				//判断用户权限部门、角色
				if(flowtemplatedetail.getDepartment().equals(personnel.getDepartment())) {
					long ruleno = flowtemplatedetail.getRule().getRuleno();
					String staffcode = personnel.getStaffcode();
					
					obj = permissionService.findPersonneltorulelink(ruleno, staffcode);
				}
				
				applydept.setFlowtemplatedetail(flowtemplatedetail);//默认1为保存，从1开始
				applydept.setDatadictByStatus(flowtemplatedetail.getDatadictByWorkflowstatusno());//保存状态,默认
				applydept.setDatadictByBranch(flowtemplatedetail.getDatadictByBranch());//是否分支 :流程1时，默认取1分支
			}
		}
		if(!"admin".equals(personnel.getLoginname()))
		if(obj == null) { //部门或者角色权限至少有一个不符合
			throw new RuntimeException("you have no permission for this deal.");
		}
		
		applydeptService.addApplydept(applydept);
		return SUCCESS;
	}
	
	public String findAllApplydept() throws Exception {
		pm = applydeptService.findApplydeptForPager();
		return SUCCESS;
	}
	
	public String findApplydept() throws Exception {
		applydept = applydeptService.findApplydept(applyno);
		return SUCCESS;
	}
	
	public String delApplydept() throws Exception {
		applydeptService.removeApplydept(applyno);
		return SUCCESS;
	}
	
	public String getApplydeptForUpdate() throws Exception {
		applydept = applydeptService.findApplydept(applyno);
		return SUCCESS;
	}
	
	public String updateApplydept() throws Exception {
		Applydept apply = null;
		apply = (Applydept) applydeptService.findApplydept(applydept.getApplyno());
		
		if(apply == null ) {
			throw new RuntimeException("has found no instance ");
		}
		
		apply.setDatadictByUnit(applydept.getDatadictByUnit());
		apply.setDatadictByPri(applydept.getDatadictByPri());
		apply.setDepartmentByDepartmentDeptcode(applydept.getDepartmentByDepartmentDeptcode());
		apply.setLimitday(applydept.getLimitday());
		apply.setRemark(applydept.getRemark());
		applydeptService.updateApplydept(apply);
		return SUCCESS;
	}

	/**
	 * @return the applydept
	 */
	public Applydept getApplydept() {
		return applydept;
	}

	/**
	 * @param applydept the applydept to set
	 */
	public void setApplydept(Applydept applydept) {
		this.applydept = applydept;
	}

	/**
	 * @return the applydeptService
	 */
	public ApplydeptService getApplydeptService() {
		return applydeptService;
	}

	/**
	 * @param applydeptService the applydeptService to set
	 */
	public void setApplydeptService(ApplydeptService applydeptService) {
		this.applydeptService = applydeptService;
	}

	/**
	 * @return the applyno
	 */
	public String getApplyno() {
		return applyno;
	}

	/**
	 * @param applyno the applyno to set
	 */
	public void setApplyno(String applyno) {
		this.applyno = applyno;
	}

	/**
	 * @return the datadictByMarklist
	 */
	public List<Datadict> getDatadictByMarklist() {
		return datadictByMarklist;
	}

	/**
	 * @param datadictByMarklist the datadictByMarklist to set
	 */
	public void setDatadictByMarklist(List<Datadict> datadictByMarklist) {
		this.datadictByMarklist = datadictByMarklist;
	}

	/**
	 * @return the datadictByPrilist
	 */
	public List<Datadict> getDatadictByPrilist() {
		return datadictByPrilist;
	}

	/**
	 * @param datadictByPrilist the datadictByPrilist to set
	 */
	public void setDatadictByPrilist(List<Datadict> datadictByPrilist) {
		this.datadictByPrilist = datadictByPrilist;
	}

	/**
	 * @return the datadictByStatuslist
	 */
	public List<Datadict> getDatadictByStatuslist() {
		return datadictByStatuslist;
	}

	/**
	 * @param datadictByStatuslist the datadictByStatuslist to set
	 */
	public void setDatadictByStatuslist(List<Datadict> datadictByStatuslist) {
		this.datadictByStatuslist = datadictByStatuslist;
	}

	/**
	 * @return the datadictByUnitlist
	 */
	public List<Datadict> getDatadictByUnitlist() {
		return datadictByUnitlist;
	}

	/**
	 * @param datadictByUnitlist the datadictByUnitlist to set
	 */
	public void setDatadictByUnitlist(List<Datadict> datadictByUnitlist) {
		this.datadictByUnitlist = datadictByUnitlist;
	}

	/**
	 * @return the departmentByDepartmentDeptcodelist
	 */
	public List<Department> getDepartmentByDepartmentDeptcodelist() {
		return departmentByDepartmentDeptcodelist;
	}

	/**
	 * @param departmentByDepartmentDeptcodelist the departmentByDepartmentDeptcodelist to set
	 */
	public void setDepartmentByDepartmentDeptcodelist(
			List<Department> departmentByDepartmentDeptcodelist) {
		this.departmentByDepartmentDeptcodelist = departmentByDepartmentDeptcodelist;
	}

	/**
	 * @return the departmentService
	 */
	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	/**
	 * @param departmentService the departmentService to set
	 */
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	/**
	 * @return the deptcode
	 */
	public String getDeptcode() {
		return deptcode;
	}

	/**
	 * @param deptcode the deptcode to set
	 */
	public void setDeptcode(String deptcode) {
		this.deptcode = deptcode;
	}

	/**
	 * @return the dictService
	 */
	public DictService getDictService() {
		return dictService;
	}

	/**
	 * @param dictService the dictService to set
	 */
	public void setDictService(DictService dictService) {
		this.dictService = dictService;
	}

	/**
	 * @return the flowworkitemService
	 */
	public FlowworkitemService getFlowworkitemService() {
		return flowworkitemService;
	}

	/**
	 * @param flowworkitemService the flowworkitemService to set
	 */
	public void setFlowworkitemService(FlowworkitemService flowworkitemService) {
		this.flowworkitemService = flowworkitemService;
	}

	/**
	 * @return the pm
	 */
	public PagerModel getPm() {
		return pm;
	}

	/**
	 * @param pm the pm to set
	 */
	public void setPm(PagerModel pm) {
		this.pm = pm;
	}

	/**
	 * @return the cardmaintypelist
	 */
	public List<Cardmaintype> getCardmaintypelist() {
		return cardmaintypelist;
	}

	/**
	 * @param cardmaintypelist the cardmaintypelist to set
	 */
	public void setCardmaintypelist(List<Cardmaintype> cardmaintypelist) {
		this.cardmaintypelist = cardmaintypelist;
	}

	/**
	 * @return the cardTypeService
	 */
	public CardTypeService getCardTypeService() {
		return cardTypeService;
	}

	/**
	 * @param cardTypeService the cardTypeService to set
	 */
	public void setCardTypeService(CardTypeService cardTypeService) {
		this.cardTypeService = cardTypeService;
	}

	/**
	 * @return the flowaudithistoryService
	 */
	public FlowaudithistoryService getFlowaudithistoryService() {
		return flowaudithistoryService;
	}

	/**
	 * @param flowaudithistoryService the flowaudithistoryService to set
	 */
	public void setFlowaudithistoryService(
			FlowaudithistoryService flowaudithistoryService) {
		this.flowaudithistoryService = flowaudithistoryService;
	}

	/**
	 * @return the permissionService
	 */
	public PermissionService getPermissionService() {
		return permissionService;
	}

	/**
	 * @param permissionService the permissionService to set
	 */
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}
	
}
